<?php 
   if(file_exists('functions.php')) {
      require_once 'functions.php';
   }
 ?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Login</TITLE>
      <meta charset="utf8"/>
      <link rel="stylesheet" href="css/style.css">
   </HEAD>
   <BODY>

      <div class="main box">
         <div class="content box">       
      		<!-- <a href="logout.php">Излез от профила</a><br> -->
            <a href="registration.php">Регистрация</a><br>
      		<form action="login.php" method="POST">
      			<div>Username:<input type="text" name="user"/></div>
      			<div>Password:<input type="password" name="pass"/></div>
      			<input type="submit" value="Влез"/>
      		</form>	

         </div>
      </div>
   		<?php 

   			if($_POST) {

   				$user = mysqli_real_escape_string($db, filter_data($_POST['user']));
   				$pass = mysqli_real_escape_string($db, filter_data($_POST['pass']));

   				$sql = "SELECT user_id, user, pass FROM user";
   				$result = mysqli_query($db, $sql);
   				while($row = $result->fetch_array()) {
   					if($row['user'] === $user && $row['pass'] === md5($pass)) {
   						$_SESSION['isLogged'] = true;
                     $_SESSION['user_id'] = $row['user_id'];
   						header("Location: traktorSystem.php");
   						mysqli_close($db);
   					}
   				}
   			}
   		 ?>
   </BODY>
</HTML>